package cn.wikiflyer.beikang.act.user;

import java.lang.reflect.Type;

import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.bean.MyOrderListBean;
import cn.wikiflyer.beikang.bean.MyOrderListBean.MyOrderBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wikiflyer.beikang.view.StatusView;
import cn.wikiflyer.beikang.view.StatusView.StatusOnClickListener;
import cn.wk.libs4a.utils.WKTimeUtils;
import cn.wk.libs4a.view.WKHeader;
import cn.wk.libs4a.view.list.WKListViewInterface;
import cn.wk.libs4a.view.list.WKListViewUtils;
import cn.wk.libs4a.view.list.WKViewHolder;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * 我的订单
 * 
 * @author qunlee_mr
 * 
 */
public class UserOrderListAct extends BaseActivity implements
		WKListViewInterface, OnItemClickListener {

	@ViewInject(id = R.id.header)
	private WKHeader header;

	private WKListViewUtils<MyOrderBean, MyOrderListBean> listUtils;

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_order_list_layout);// common_layout布局作为列表通用布局
	}

	@Override
	public void init() {
		header.setTitle("我的订单");
		header.setLeftAsBack();
		initList();
	}

	@Override
	public void setData() {
		listUtils.updateData();
	}

	private void initList() {
		PullToRefreshListView pList = (PullToRefreshListView) findViewById(R.id.wk_pull_refresh_list);
		listUtils = new WKListViewUtils<MyOrderBean, MyOrderListBean>() {
		};
		listUtils.init(ctx, this, pList);
		listUtils.listView.setDividerHeight(15);
		listUtils.pullRefreshListView.setOnItemClickListener(this);
	}

	@Override
	public String getData(boolean... isLocal) {
		return app().net.getOrderList(listUtils.page, listUtils.pageCount,
				isLocal);
	}

	@Override
	public void getDataBack(String result, boolean isLocal) {
		Type cvbType = new TypeToken<BKBaseBean<MyOrderListBean>>() {
		}.getType();
		Gson gson = new Gson();
		BKBaseBean<MyOrderListBean> base = gson.fromJson(result, cvbType);
		if (base.status == 1) {
			listUtils.bean = base;
			listUtils.list.addAll(base.data.list);
			listUtils.adapter.notifyDataSetChanged();
		} else {
			Toast.makeText(ctx, "获取数据失败" + base.info, 0).show();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	@Override
	public WKViewHolder createViewHolder(View arg1) {
		return new MyViewHolder(arg1);
	}

	@Override
	public int getItemViewId() {
		return R.layout.act_user_order_list_item;
	}

	class MyViewHolder extends WKViewHolder<MyOrderBean> {

		TextView order_code;
		TextView order_good;
		TextView order_time;
		StatusView order_status;
		LinearLayout ll_item;
		TextView order_count;

		public MyViewHolder(View view) {
			ll_item = (LinearLayout) view.findViewById(R.id.ll_item);
			order_code = (TextView) view.findViewById(R.id.orderitem_code);
			order_good = (TextView) view.findViewById(R.id.orderitem_goodname);
			order_time = (TextView) view.findViewById(R.id.orderitem_time);
			order_status = (StatusView) view
					.findViewById(R.id.orderitem_status);
			ll_item = (LinearLayout) view.findViewById(R.id.ll_item);
			order_count = (TextView) view.findViewById(R.id.orderitem_count);
		}

		@Override
		public void setData(final MyOrderBean t) {
			order_code.setText(t.orderno);
			String titles = "";
			for (int i = 0; i < t.goods.size(); i++) {
				titles += t.goods.get(i).title;
			}
			// 商品
			order_good.setText(titles);
			// 共 X 5 共3455元。
			order_count.setText(t.quantity + "盒，共" + t.total + "元");

			order_time.setText(WKTimeUtils.formatTime(t.ordertime));

			if (t.is_receipt != null && t.is_receipt.equals("1")) {
				order_status.setStatus(99);
			} else {
				order_status.setStatus(t.pay_status);
			}
			order_status.setClickListener(new StatusOnClickListener() {
				@Override
				public void click(int status) {
					nextActivity(status, t.orderno);
				}
			});

			ll_item.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					if (t.is_receipt != null && t.is_receipt.equals("1")) {
						nextActivity(99, t.orderno);
					} else {
						nextActivity(t.pay_status, t.orderno);
					}
				}
			});
		}

	}

	@Override
	protected void onRestart() {
		super.onRestart();
		System.out.println("lazy  on restart start");
		listUtils.updateData();
	}

	/**
	 * 跳转下一个界面
	 */
	private void nextActivity(int status, String orderno) {
		if (!TextUtils.isEmpty(orderno)) {
			Intent intent = new Intent(ctx, UserOrderInfoAct.class);
			intent.putExtra("status", status);
			intent.putExtra("orderno", "" + orderno);
			startActivity(intent);
		} else {
			Toast.makeText(ctx, "没有订单号", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// Intent intent = new Intent(ctx, UserOrderInfoAct.class);
		// intent.putExtra("orderno", listUtils.list.get(arg2 - 1).id);
		// startActivity(intent);
	}

}
